<template>
  <!-- 外层容器 -->
  <div class="dong-collapse-item" :class="{ 'is-disabled': disabled }">
    <!-- 标题部分 -->
    <div class="dong-collapse-item__header" :class="{
      'is-disabled': disabled,
      'is-active': isActive,
    }" :id="`item-header-${name}`" @click="itemClickHandle">
      <slot name="title">{{ title }}</slot>
      <Dong-Icon icon="angle-right" class="header-angle" />
    </div>
    <!-- 内容部分 -->
    <transition name="slide" v-on="transitionEvents">
      <div class="dong-collapse-item__wrapper" v-show="isActive">
        <div class="dong-collapse-item__content" :id="`item-content-${title}`">
          <slot />
        </div>
      </div>
    </transition>
  </div>
</template>

<script setup>
import { inject, computed } from 'vue'
import { collapseItemProps, collapseContextKey } from './props'
defineOptions({ name: 'DongCollapseItem' })
const props = defineProps(collapseItemProps)
const collapseContext = inject(collapseContextKey)
// 接下来我们需要有一个计算属性来计算是否是激活状态
// 如果当前 CollapseItem 的 name 值存在于 Collapse 组件提供的 activeNames 数组中
// 说明当前项目是激活状态
const isActive = computed(() => collapseContext.activeNames.value.includes(props.name))
const itemClickHandle = () => {
  if (props.disabled) return
  collapseContext.handleItemClick(props.name)
}

// 在这个对象里面，会指定过渡开始和结束时的不同样式
const transitionEvents = {
  beforeEnter(el) {
    el.style.height = '0px'
    el.style.overflow = 'hidden'
  },
  enter(el) {
    el.style.height = el.scrollHeight + 'px'
  },
  afterEnter(el) {
    el.style.height = ''
    el.style.overflow = ''
  },
  beforeLeave(el) {
    el.style.height = el.scrollHeight + 'px'
    el.style.overflow = 'hidden'
  },
  leave(el) {
    el.style.height = '0px'
  },
  afterLeave(el) {
    el.style.height = ''
    el.style.overflow = ''
  }
}
</script>

<style lang="scss" scoped></style>
